﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace GoShopping.Web.SystemManager
{
    public partial class AdvisoryList : System.Web.UI.Page
    {
        BLL.SysInfo.AdvisoryBean ab = new GoShopping.BLL.SysInfo.AdvisoryBean();
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.PrdtButton.Visible = false;
            Master.SupplierButton.Visible = false;
            if (!IsPostBack)
            {
                //Master.ElementSelectedChange = GetTreeView;
                GetGVData(null);
            }
        }

        private void GetData()
        {
            BLL.UserBean ub = new GoShopping.BLL.UserBean();
            BLL.UserManagerBean umb = new GoShopping.BLL.UserManagerBean();
        }
        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("~/ProductManager/Navigation.xml"), "sys");
        }
        void GetGVData(string filterString)
        {
            if (string.IsNullOrEmpty(filterString))
            {
                extGVAdvisory.DataSource = ab.GetAllAdvisory();
                extGVAdvisory.DataBind();
            }
            else
            {
                extGVAdvisory.DataSource = ab.GetAdvisoryList("and ProductID in (SELECT ID FROM Product WHERE ProductTypeID=" + filterString + ")");
                extGVAdvisory.DataBind();
            }
            #region Temp
            //string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["GoShoppingConnectionString"].ConnectionString;// @"Data Source=RAY\SQLEXPRESS;Initial Catalog=GoShopping;Persist Security Info=True;User ID=sa;Password=1984326z";
            //System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(connStr);
            //System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();
            //sqlCmd.Connection = sqlConn;
            //sqlConn.Open();
            //sqlCmd.CommandText = "select * from dbo.Advisory";
            ////sqlCmd.CommandText = "SELECT dbo.ProductType.ID, dbo.ProductType.TypeName, dbo.ProductType.ParentID, dbo.ProductType.Remark, + "
            ////          + "dbo.ProductType_RelateColumn.ProductTypeID, dbo.ProductType_RelateColumn.PropertyChName, "
            ////          + "dbo.ProductType_RelateColumn.PropertyColumnName, dbo.ProductType_RelateColumn.ColumnType, "
            ////          + "dbo.ProductType_RelateColumn.SelectTypeValue "
            ////          + "FROM dbo.ProductType INNER JOIN "
            ////          + " dbo.ProductType_RelateColumn ON dbo.ProductType.ID = dbo.ProductType_RelateColumn.ProductTypeID";
            //System.Data.SqlClient.SqlDataAdapter sqlDa = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
            //DataSet ds = new DataSet();
            //sqlDa.Fill(ds);
            //sqlConn.Close();
            #endregion
        }

        void GetDVData(int id)
        {
            dtReply.DataSource = ab.GetAdvisoryList("and id=" + id);
            dtReply.DataBind();

            #region Temp
                //string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["GoShoppingConnectionString"].ConnectionString;// @"Data Source=RAY\SQLEXPRESS;Initial Catalog=GoShopping;Persist Security Info=True;User ID=sa;Password=1984326z";
                //System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(connStr);
                //System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();
                //sqlCmd.Connection = sqlConn;
                //sqlConn.Open();
                //sqlCmd.CommandText = "select * from dbo.Advisory where id=" + id;
                ////sqlCmd.CommandText = "SELECT dbo.ProductType.ID, dbo.ProductType.TypeName, dbo.ProductType.ParentID, dbo.ProductType.Remark, + "
                ////          + "dbo.ProductType_RelateColumn.ProductTypeID, dbo.ProductType_RelateColumn.PropertyChName, "
                ////          + "dbo.ProductType_RelateColumn.PropertyColumnName, dbo.ProductType_RelateColumn.ColumnType, "
                ////          + "dbo.ProductType_RelateColumn.SelectTypeValue "
                ////          + "FROM dbo.ProductType INNER JOIN "
                ////          + " dbo.ProductType_RelateColumn ON dbo.ProductType.ID = dbo.ProductType_RelateColumn.ProductTypeID";
                //System.Data.SqlClient.SqlDataAdapter sqlDa = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
                //DataSet ds = new DataSet();
                //sqlDa.Fill(ds);
                //sqlConn.Close();
                #endregion
        }
        protected void extGVAdvisory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.extGVAdvisory.PageIndex = e.NewPageIndex;
            //GetGVData(ddlPrdtType.SelectedValue);
            GetGVData(null);
        }

        protected void extGVAdvisory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "IsReply")
            {
                GetDVData(Convert.ToInt32(e.CommandArgument));
            }
            else if (e.CommandName == "Cover")
            {
                try
                {
                    ab.CoverOneAdvisory(Convert.ToInt32(e.CommandArgument), true);
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('屏蔽成功!')</script>", false);
                    GetGVData(null);
                }
                catch
                {
                }
            }
        }

        protected void dtReply_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == "Reply")
            {
                try
                {
                    ab.ReplyOneAdvisory(Convert.ToInt32(e.CommandArgument), ((TextBox)dtReply.FindControl("zlTxtReplyContent")).Text, Context.User.Identity.Name);
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('回复成功!')</script>", false);
                }
                catch
                { }
            }
        }

        protected void ddlPrdtType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //GetGVData(ddlPrdtType.SelectedValue);
            GetGVData(null);
        }
        protected string CheckReplyer(object replyerID)
        {
            BLL.UserBean ub = new GoShopping.BLL.UserBean();
            if (replyerID == null)
            {
                return "";
            }
            string userName = ub.GetUserList("and id=" + Convert.ToInt16(replyerID)).ElementAt<Model.UserInfo>(0).Name;
            return userName;
        }

        //void GetDDLData()
        //{
        //    SetControlCommon.GetDDLType();
        //    #region past
        //    //ddlPrdtType.Items.Clear();
        //    //BLL.ProductTypeBean ptb = new GoShopping.BLL.ProductTypeBean();
        //    //ddlPrdtType.Items.Add(new ListItem("所有", null));
        //    //ddlPrdtType.AutoPostBack = true;
        //    //foreach (GoShopping.Model.ProductType pt in ptb.GetAllProductTypeList())
        //    //{
        //    //    ListItem li = new ListItem(pt.TypeName, pt.ID.ToString());
        //    //    ddlPrdtType.Items.Add(li);
        //    //}
        //    #endregion
        //}
        protected void extGVAdvisory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //string ltr = e.Row.Cells[5].Text;//FindControl("ltrReply") as Literal;
            //if (ltr.Text == "")
            //{
            //    ltr.Text = "尚未回复.";
            //}
        }
}
}
